//
//  LKHomeViewController.swift
//  LKWeibo
//
//  Created by like on 16/8/9.
//  Copyright © 2016年 like. All rights reserved.
//

import UIKit
// cell可重用标示符
private let LKHomeViewCellId = "LKHomeViewCellId"

class LKHomeViewController: LKVisitorViewController {
    
    //创建一个viewmodel对象
    private lazy var statusListViewModel: LKStatusListViewModel = LKStatusListViewModel()
    
//     刷新控件
    let refreshControl = LKRefreshControl()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !isLogin {
            visitorView?.setupVisitorViewInfo(nil, title: nil)
            return
        }else{
            setupTableViewInfo()
            loadData()
        }
    }
    
    func setupTableViewInfo(){
        tableView.registerClass(LKStatusCell.self, forCellReuseIdentifier: LKHomeViewCellId)
        tableView.dataSource = self
        tableView.delegate = self
        
//        tableView.rowHeight = 200
        
        //设置自动计算行高
        tableView.rowHeight = UITableViewAutomaticDimension
        //设置预估行高
        tableView.estimatedRowHeight = 200
        
        
        tableView.tableFooterView = activityView

        
        
        // 添加事件
        refreshControl.addTarget(self, action: #selector(LKHomeViewController.refreshAction), forControlEvents: UIControlEvents.ValueChanged)
        // 添加到tableView身上
        tableView.addSubview(refreshControl)
        tableView.separatorStyle = .None
        
        //监听通知 
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LKHomeViewController.reloadTable), name: "reloadTableView", object: nil)
        
    }
    
    
    func reloadTable(){
//        printLog("刷新数据")
        self.tableView.setContentOffset(CGPoint(x: 0, y: -64), animated: false)
        loadData()
    }
    
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
//    
    func refreshAction(){
        printLog("请求数据")
        loadData()
    }
    
    
    //footview上的菊花
    private lazy var activityView: UIActivityIndicatorView = {
        
        let activity = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        activity.color = appThemeColor
        
        
        return activity
        
    }()
    
    
    
    
}


extension LKHomeViewController:UITableViewDataSource,UITableViewDelegate{
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
//        printLog(statusListViewModel.dataArray.count)
        
        return statusListViewModel.dataArray.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(LKHomeViewCellId, forIndexPath: indexPath) as! LKStatusCell
        
        cell.statusViewModel = statusListViewModel.dataArray[indexPath.row]
        
        return cell;
        
    }
    
    
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
 
        // 如果是最后一个cell  而且菊花没有动画
        if indexPath.row + 1 == statusListViewModel.dataArray.count && !activityView.isAnimating() {
            // 设置动画
            activityView.startAnimating()
            printLog(indexPath.row)
            // 请求数据
            loadData()
        }
    }
  
}

extension LKHomeViewController {
    func loadData(){
        // 请求数据 true 为上拉加载更多   false  下拉刷新
        statusListViewModel.loadData(activityView.isAnimating()) { (isSuccess) -> () in
            
            self.endRefreshing()
            
            if !isSuccess {
                print("请求数据失败")
                return
            }
            
            // 请求数据成功
            printLog("请求数据成功")
            // 刷新UI
            self.tableView.reloadData()
        }
    }
    // 停止动画
    func endRefreshing(){
        self.activityView.stopAnimating()
        self.refreshControl.endRefreshing()
    }
}

